/******************************************************************************
 * @File name   :      SecondLevelCacheMgntController.java
 *
 * @Author      :      ZIZOU WANG
 *
 * @Date        :      Dec 20, 2011
 *
 * @Copyright Notice: 
 * Copyright (c) 2014 DMS, Inc. All  Rights Reserved.
 * This software is published under the terms of the DMS Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * Dec 20, 2011 11:02:24 AM        ZIZOU WANG     1.0            Initial Version
 *****************************************************************************/
package com.cap.bts.framework.common.controller;

import org.hibernate.stat.Statistics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cap.bts.commonfunc.common.service.IErrorCodeService;
import com.cap.bts.framework.cache.service.ISecondLevelCacheService;
import com.cap.bts.framework.common.controller.BaseController;
import com.cap.bts.framework.common.exception.AppException;

/**
 *äºŒçº§ç¼“å­˜æ¸…ç�†
 */
@Controller
@RequestMapping(value = "/secondLevelCache")
public class SecondLevelCacheMgntController extends BaseController {
	
	@Autowired
	ISecondLevelCacheService secondLevelCacheService;
	@Autowired
	IErrorCodeService errorCodeService;
	
	/**
	 * æ¸…ç©ºäºŒçº§ç¼“å­˜æ‰€æœ‰å†…å®¹
	 * @Date        :      Dec 20, 2011
	 * @param model
	 * @return
	 * @throws AppException
	 */
	@RequestMapping(value = "/clearAll", method = RequestMethod.GET)
	public String clearAll(Model model) throws AppException {
		secondLevelCacheService.clearAllInJpaSecondLevelCache();
		return "success";
	}
	
	/**
	 * æ¸…ç©ºäºŒçº§ç¼“å­˜æŸ�ä¸ªdomain
	 * @Date        :      Dec 20, 2011
	 * @param className
	 * @param model
	 * @return
	 * @throws AppException
	 */
	@RequestMapping(value = "/clearDomain", method = RequestMethod.GET)
	public String clearDomain(@RequestParam("className") String className,Model model) throws AppException {
		secondLevelCacheService.clearDomainInJpaSecondLevelCache(className);
		return "success";
	}
	
	/**
	 * æ¸…ç©ºäºŒçº§ç¼“å­˜æŸ�ä¸ªdomainçš„æŸ�è¡Œ
	 * @Date        :      Dec 20, 2011
	 * @param className
	 * @param domainId
	 * @param model
	 * @return
	 * @throws AppException
	 */
	@RequestMapping(value = "/clearDomainRow", method = RequestMethod.GET)
	public String clearDomainRow(@RequestParam("className") String className,
			@RequestParam("domainId") Long domainId, Model model) throws AppException {
		secondLevelCacheService.clearDomainRowInJpaSecondLevelCache(className, domainId);
		return "success";
	}
	/**
	 * get statistics
	 * @Date        :      Dec 20, 2011
	 * @param className
	 * @param domainId
	 * @param model
	 * @return
	 * @throws AppException
	 */
	@RequestMapping(value = "/getStatistics" )
	@ResponseBody
	public String getStatistics(Model model) throws AppException {
        StringBuffer ret=new StringBuffer();;
        Statistics statistics=errorCodeService.getStatistic();    
        long hitCountRatio=0L;
        if(statistics!=null){
            ret.append("Hit Count:");
            ret.append(statistics.getSecondLevelCacheHitCount());
            ret.append(",Miss Count:");
            ret.append(statistics.getSecondLevelCacheMissCount());
            ret.append(",Hit Ratio:");
            if(statistics.getSecondLevelCacheHitCount()+statistics.getSecondLevelCacheMissCount()>0){
                hitCountRatio=statistics.getSecondLevelCacheHitCount()*100/
                    (statistics.getSecondLevelCacheHitCount()+statistics.getSecondLevelCacheMissCount());               
            }
            ret.append(hitCountRatio);
            ret.append(";");
        }else{
            ret.append("Hit Ratioï¼š0;(check your statistics swith is open)");
        }
        return ret.toString();
    }
	
}
